package com.hotau.account;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 付费管理
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-11
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
public class PayManageFragment extends Fragment implements OnClickListener {

	private Button pay_manager_btn_payment_card = null;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.account_pay_manage, container,false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		HotauActivity csmbpActivity = (HotauActivity) getActivity();
		switch (v.getId()) {
		case R.id.account_pay_manager_btn_payment_card:
			HttpRequestAsynTask payment_isbind_task = new HttpRequestAsynTask(getActivity()){
				HotauActivity csmbpActivity = (HotauActivity) getActivity();
				@Override
				protected void doPostExecute(String result) {
					try {
						JSONObject bindCardJson = new JSONObject(result);
						if(bindCardJson != null && bindCardJson.getInt("result") == 1){
							//成功
							JSONArray cards = bindCardJson.getJSONArray("cardInfoList");
							Bundle b1 = new Bundle();
							b1.putBoolean("isBind", true);
							b1.putSerializable("jsonData", cards.toString());
							PaymentAdvanceManageFragment paf = new PaymentAdvanceManageFragment();
							csmbpActivity.pushFragment("预付卡管理 ", paf);
						}else if(bindCardJson != null && bindCardJson.getInt("result") == 2){
							Bundle b2 = new Bundle();
							b2.putBoolean("isBind", true);
							b2.putSerializable("jsonData", null);
							PaymentAdvanceManageFragment paf1 = new PaymentAdvanceManageFragment();
							csmbpActivity.pushFragment("预付卡管理 ", paf1);
						}else{
							//失败
							DialogUtil.showDialog(getActivity(), "", bindCardJson.getString("errorMsg"));
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						DialogUtil.showDialog(getActivity(), "", "获取绑定预付卡返回数据出错 ");
					}
				}
			};
			StringBuilder data = new StringBuilder();
			data.append("Form:token="+MemberService.getToken(getActivity()));
			data.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
			data.append(";sing="+"");
			payment_isbind_task.execute(HotauConstants.ACCOUNT_PAYMENT_BIND_URL,data.toString(),HttpUtil.UTF8_ENCODING);
			break;
		default:
			break;
		}
	}
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget() {
		pay_manager_btn_payment_card = (Button) getActivity().findViewById(R.id.account_pay_manager_btn_payment_card);
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		pay_manager_btn_payment_card.setOnClickListener(this);
	}

}
